<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.title" placeholder="标题/作者/来源" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      @sort-change="sortChange"
    >
      <el-table-column label="名称" prop="id" sortable="custom" align="center" min-width="140" :class-name="getSortClass('id')">
        <template slot-scope="{row}">
          <span>{{ row.sn }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" max-width="150px">
        <template slot-scope="{row}">
          <span class="link-type" @click="handleShow(row)">{{ row.title }}</span>
        </template>
      </el-table-column>
      <el-table-column label="触发器" class-name="status-col" width="100">
        <template slot-scope="{row}">
          <el-tag :type="row.status">
            {{ row.status.label }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="下次运行时间" class-name="status-col" width="180">
        <template slot-scope="{row}">
            {{ row.updated_at }}
        </template>
      </el-table-column>
      <el-table-column label="上次运行时间" class-name="status-col" width="180">
        <template slot-scope="{row}">
            {{ row.updated_at }}
        </template>
      </el-table-column>
      <el-table-column label="上次运行结果" class-name="status-col" width="180">
        <template slot-scope="{row}">
            {{ row.updated_at }}
        </template>
      </el-table-column>
      <el-table-column label="创建者" class-name="status-col" width="180">
        <template slot-scope="{row}">
            {{ row.updated_at }}
        </template>
      </el-table-column>
      <el-table-column label="创建时间" class-name="status-col" width="180">
        <template slot-scope="{row}">
            {{ row.updated_at }}
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.start" :limit.sync="listQuery.perpage" @pagination="getList" />

  </div>
</template>

<script>
import { getMissionList  } from '@/api/foundation/mission'
import waves from '@/directive/waves' // waves directive
import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'knowledgeAdmin',
  components: { Pagination },
  directives: { waves },
  filters: {
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        start: 1,
        perpage: 10,
        title: undefined,
        sort: '+id'
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      getMissionList(this.listQuery).then(response => {
        this.list = response.data.list
        this.total = response.data.total

        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    },
    handleShow:function(item){
      this.$router.push({ path: 'mission-detail', query: { 'knowledgeId': item.identity }})
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    sortChange(data) {
      const { prop, order } = data
      if (prop === 'id') {
        this.sortByID(order)
      }
    },
    sortByID(order) {
      if (order === 'ascending') {
        this.listQuery.sort = '+id'
      } else {
        this.listQuery.sort = '-id'
      }
      this.handleFilter()
    },
    formatJson(filterVal) {
      return this.list.map(v => filterVal.map(j => {
        if (j === 'timestamp') {
          return parseTime(v[j])
        } else {
          return v[j]
        }
      }))
    },
    getSortClass: function(key) {
      const sort = this.listQuery.sort
      return sort === `+${key}` ? 'ascending' : 'descending'
    }
  }
}
</script>
